from typing import Any, Dict
from django.forms.models import BaseModelForm
from django.http import HttpResponse
from django.views.generic import ListView, DetailView, CreateView, DeleteView, UpdateView
from django.contrib.auth.mixins import LoginRequiredMixin
from server.apps.blog.infrastructure import forms
from server.apps.blog.models import Post
from django.urls import reverse_lazy



class PostShowView(ListView):
    template_name = 'blog/index.html'
    model = Post


class CreatePostView(LoginRequiredMixin, CreateView):
    template_name = 'blog/create.html'
    model = Post
    form_class = forms.FriendlyPostForm
    success_url = reverse_lazy('blog:list')
    
    def form_valid(self, form: BaseModelForm) -> HttpResponse:
        obj = form.save(commit=False)
        obj.author = self.request.user
        obj.save()
        return super().form_valid(form)


class PostDetailShowView(DetailView):
    template_name = 'blog/post.html'
    model = Post
    
class PostEditView(LoginRequiredMixin, UpdateView):
    template_name = 'blog/edit.html'
    model = Post
    form_class = forms.FriendlyPostForm
    success_url = reverse_lazy('blog:list')

class PostDeleteView(LoginRequiredMixin, DeleteView):
    template_name = 'blog/post.html'
    model = Post
    success_url = reverse_lazy('blog:list')


class MyPostShowView(LoginRequiredMixin, ListView):
    template_name = 'blog/index.html'
    model = Post
    def get_queryset(self):
        return self.model.objects.filter(author=self.request.user)
    
